package com.redare.dev.smart.generator.workbench.controller;

import com.redare.dev.smart.generator.sdk.CommonSdk;
import com.redare.dev.smart.generator.sdk.OnlFormHeadSdk;
import com.redare.dev.smart.generator.sdk.notice.ICodeGenPullListener;
import com.redare.dev.smart.generator.sdk.pojo.*;
import com.redare.dev.smart.generator.sdk.pojo.form.OnlFormHeadForm;
import com.redare.devframework.common.pojo.CommonResult;
import com.redare.devframework.common.pojo.Page;
import com.redare.devframework.webplatform.workbench.conf.annotations.WebFun;
import com.redare.devframework.webplatform.workbench.controller.component.BaseWebController;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * @date 2021-08-28 11:44:05
 * Created by yangqiangyu
 */


@RequestMapping(value = "/api/_generator_api/onlFormHead")
@Controller
public class OnlFormHeadController extends BaseWebController {

    @Autowired
    OnlFormHeadSdk onlFormHeadSdk;

    @Autowired
    CommonSdk commonSdk;

    @Autowired(required = false)
    ICodeGenPullListener listener;



    @ResponseBody
    @RequestMapping(value = "/code/preview", method = RequestMethod.POST)
    @WebFun(classifyName = "表单管理", id = "codePreview", name = "代码生成")
    public CommonResult<String> codePreview(
            @RequestBody CodeGenForm.CodePreview form
    ) {
        return commonSdk.codePreview(form);
    }


    @ResponseBody
    @RequestMapping(value = "/code/generate", method = RequestMethod.POST)
    @WebFun(classifyName = "表单管理", id = "codeGenerate", name = "代码生成")
    public CommonResult codeGenerate(
            @RequestBody CodeGenForm.CodeGen form
    ) {
        return commonSdk.codeGenerate(form);
    }



    @ResponseBody
    @RequestMapping(
            value = {"/getCodeGroupGenPath"},
            method = {RequestMethod.POST}
    )
    public CommonResult<GenPath> getCodeGroupGenPath(@RequestBody CodeGroup form) {
        if (this.listener==null){
            return CommonResult.returnFailsWrapper("生成路径监听器未配置");
        }
        return this.listener.getCodeGroupGenPath(form);
    }


    @ResponseBody
    @RequestMapping(value = "/getCodeRoots", method = RequestMethod.POST)
    @WebFun(classifyName = "表单管理", id = "getCodeRoots", name = "sql解析")
    public CommonResult<List<CodeRoot>> getCodeRoots(
    ) {
        CommonResult<List<CodeRoot>> commonResult = commonSdk.getCodeRoots();
        if (commonResult.isNotSuccess()){
            return commonResult;
        }
        List<CodeRoot> codeRoots = commonResult.getData();
        if (CollectionUtils.isNotEmpty(codeRoots)){
            List<CodeRoot> returnList = new ArrayList<>();
            for (CodeRoot codeRoot : codeRoots) {
                CodeRoot newRoot = new CodeRoot();
                BeanUtils.copyProperties(codeRoot,newRoot);
                returnList.add(newRoot);
            }
            for (CodeRoot codeRoot : returnList) {
                for (CodeGroup group : codeRoot.getGroups()) {
                    for (CodeTemplate template : group.getTemplates()) {
                        template.setTemplate("");
                    }
                }
            }
            commonResult.setData(returnList);
        }
        return commonResult;
    }

    @ResponseBody
    @RequestMapping(value = "/parseAdd", method = RequestMethod.POST)
    @WebFun(classifyName = "表单管理", id = "OnlSqlParse", name = "sql解析")
    public CommonResult<Boolean> parseSqlToOnlFormHead(
            @RequestBody OnlFormHeadForm.Parse form
    ) {
        return onlFormHeadSdk.parseAddOnlFormHeadByBean(form);
    }


    @ResponseBody
    @RequestMapping(value = "/syncSql", method = RequestMethod.POST)
    @WebFun(classifyName = "表单管理", id = "OnlSyncParse", name = "sql同步")
    public CommonResult<Boolean> syncSqlToOnlFormHead(
    ) {
        return onlFormHeadSdk.syncAddOnlFormHeadByBean();
    }



    @ResponseBody
    @RequestMapping(value = "/add", method = RequestMethod.POST)
    @WebFun(classifyName = "表单管理", id = "OnlFormHeadAdd", name = "表单添加")
    public CommonResult<Boolean> addOnlFormHead(
            @RequestBody OnlFormHeadForm.Add form
    ) {
        return onlFormHeadSdk.addOnlFormHead(form);
    }

    @ResponseBody
    @RequestMapping(value = "/edit", method = RequestMethod.POST)
    @WebFun(classifyName = "表单管理", id = "OnlFormHeadEdit", name = "表单编辑")
    public CommonResult<Boolean> editOnlFormHead(
            @RequestBody OnlFormHeadForm.Edit form
    ) {
        return onlFormHeadSdk.editOnlFormHead(form);
    }

    @ResponseBody
    @RequestMapping(value = "/delete", method = RequestMethod.POST)
    @WebFun(classifyName = "表单管理", id = "OnlFormHeadDel", name = "表单删除")
    public CommonResult<Boolean> deleteOnlFormHead(
            @RequestBody OnlFormHeadForm.Del form
    ) {
        return onlFormHeadSdk.deleteOnlFormHead(form);
    }

    @ResponseBody
    @RequestMapping(value = "/info", method = RequestMethod.POST)
    public CommonResult<OnlFormHead> getOnlFormHead(
            @RequestBody OnlFormHeadForm.Get form
    ) {
        form.setQueryField(true);
        return onlFormHeadSdk.getOnlFormHead(form);
    }


    @ResponseBody
    @RequestMapping(value = "/page", method = RequestMethod.POST)
    public CommonResult<Page<OnlFormHead>> findOnlFormHeadPage(
            @RequestBody OnlFormHeadForm.Search form,
            @RequestParam(value = "curPage", required = false, defaultValue = "1") int curPage,
            @RequestParam(value = "pageSize", required = false, defaultValue = "10") int pageSize
    ) {
        return onlFormHeadSdk.findOnlFormHeadPage(form, curPage, pageSize);
    }


}